package com.xushi.service.impl;

import com.xushi.dao.UserProfileRepository;
import com.xushi.dao.UserRepository;
import com.xushi.bean.State;
import com.xushi.model.User;
import com.xushi.model.UserProfile;
import com.xushi.bean.UserProfileType;
import com.xushi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userDao;

    @Autowired
    UserProfileRepository userProfileDao;

    public User findBySsoId(String username) {
        User user = userDao.findBySsoId(username);
        return user;
    }

    public User createUser(String username, String password) {
        Set<UserProfile> userProfiles = new HashSet<>();
        UserProfile userProfile = userProfileDao.findByType(UserProfileType.USER.getUserProfileType());
        userProfiles.add(userProfile);
        User user = new User(username, password, State.ACTIVE.getState(), userProfiles);
        return userDao.save(user);
    }
}
